package com.hotel.domain.service.impl;

import com.github.pagehelper.PageHelper;
import com.hotel.common.PageEntity;
import com.hotel.domain.dao.GuestsMapper;
import com.hotel.domain.dao.VipInfoMapper;
import com.hotel.domain.dao.VipMapper;
import com.hotel.domain.service.GuestsService;
import com.hotel.domain.service.VipInfoService;
import com.hotel.pojo.Guests;
import com.hotel.pojo.VipInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VipInfoServiceImpl implements VipInfoService {

    @Autowired
    private VipInfoMapper vipInfoMapper;
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private GuestsService guestsService;
    @Autowired
    private GuestsMapper guestsMapper;

    @Override
    @Transactional
    public int addVipInfo(VipInfo vipInfo) {
        //根据身份证号查看是否为客户或VIP客户
        Guests guests = new Guests();
        guests.setGuestIdcard(vipInfo.getGuestIdcard());
        List<Guests> list = guestsMapper.getGuestsList(guests);
        int n1 = 0;
        int n2 = 0;
        if(list.size() == 0){
            //还不是客户，添加客户并升级VIP
            Guests guests1 = vipInfo.getGuests();
            n1 = guestsMapper.addGuests(guests1);
            n2 = guestsService.upToVip((int)guests1.getGuestId());
        }
        return n1+n2;
    }

    @Override
    @Transactional
    public int updateVipInfo(VipInfo vipInfo) {
        return vipMapper.updateVip(vipInfo.getVip());
    }

    @Override
    public PageEntity<VipInfo> getVipInfoList(VipInfo vipInfo, int page, int perPage) {
        PageHelper.startPage(page, perPage);
        List<VipInfo> list = vipInfoMapper.getVipInfoList(vipInfo);
        int pageTotal = (int)Math.ceil(vipInfoMapper.getVipInfoNum(vipInfo)/(double)perPage);
        return new PageEntity<VipInfo>(list, page, pageTotal);
    }

}
